package team.bluepen.order.data.database.mysql;

import space.lingu.light.*;
import team.bluepen.order.data.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Kai
 */
@Dao
@SuppressWarnings("unused")
public abstract class UserDao implements DaoConnectionGetter {
    @Insert
    public abstract void insert(User user);

    @Insert
    public abstract void insert(List<User> users);

    @Query("DELETE FROM user")
    public abstract void delete();

    @SuppressWarnings("all")
    public void delete(long id) {
        SharedConnection sharedConnection = getConnection();
        Connection connection = getConnection().acquire();
        try {
            final PreparedStatement stmt = connection.prepareStatement(
                "DELETE FROM user WHERE user_id = ?");
            stmt.setLong(1, id);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new LightRuntimeException(e);
        } finally {
            sharedConnection.release(connection);
        }
    }

    @Delete
    public abstract void delete(User user);

    @Delete
    public abstract void delete(List<User> user);

    @Update
    public abstract void update(User user);

    @Update
    public abstract void update(List<User> user);

    @Query("SELECT * FROM user")
    public abstract List<User> get();

    @Query("SELECT * FROM user WHERE user_id = {id}")
    public abstract User get(long id);

    @Query("SELECT * FROM user WHERE username = {username}")
    public abstract User get(String username);
}
